﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>组件对象获取 - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The 组件对象获取 function returns a reference to an 对象 provided by a COM component." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>组件对象获取</h1>
<p>返回对 COM 组件提供的对象的引用.</p>
<pre class="Syntax">组件对象 := <span class="func">组件对象获取</span>(Name)</pre>

<h2 id="Parameters">参数</h2>
<dl>

  <dt>Name</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>要检索的对象的显示名称. 有关详情, 请参阅 <a href="http://msdn.microsoft.com/en-us/library/ms691253.aspx">MkParseDisplayName (MSDN)</a>.</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#objects">对象</a></p>
<p>此函数返回对 COM 对象的<a href="../Concepts.htm#references-to-objects">引用</a>.</p>

<h2 id="Error_Handling">错误处理</h2>
<p>失败时抛出异常.</p>

<h2 id="Related">相关</h2>
<a href="ComObjCreate.htm">组件对象创建</a>, <a href="ComObjActive.htm">组件对象激活</a>, <a href="ComObjConnect.htm">组件对象连接</a>, <a href="ComObjQuery.htm">组件对象查询</a>, <a href="http://msdn.microsoft.com/en-us/library/ms678805.aspx">CoGetObject (MSDN)</a>

<h2 id="Examples">示例</h2>
<div class="ex" id="exwmi">
<p><a href="#exwmi">#1</a>: 按下 <kbd>Shift</kbd>+<kbd>Escape</kbd> 来显示活动窗口进程的命令行.</p>
<pre>+Esc::
{
    pid := 获取窗口进程编号("A")
    <em>; 获取 WMI 服务对象.</em>
    wmi := <b>组件对象获取</b>("winmgmts:")
    <em>; 执行查询以获取匹配进程.</em>
    queryEnum := wmi.ExecQuery(""
        . "Select * from Win32_Process where ProcessId=" . pid)
        ._NewEnum()
    <em>; 获取首个匹配进程.</em>
    如果 queryEnum[proc]
        信息框(proc.CommandLine, "Command line", 0)
    否则
        信息框("Process not found!")
    <em>; 释放所有全局对象(使用局部变量时不需要这么做).</em>
    wmi := queryEnum := proc := ""
}
<em>; Win32_Process: <a href="http://msdn.microsoft.com/en-us/library/aa394372.aspx">http://msdn.microsoft.com/en-us/library/aa394372.aspx</a></em>
</pre>
</div>

</body>
</html>